package com.fstec.ks.master.dao.jdbc;

import java.util.Date;
import java.util.List;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.fstec.ks.master.dao.ExamServerDao;
import com.fstec.ks.master.dao.jdbc.extractor.ExamServerExtractor;
import com.fstec.ks.master.vo.ExamServerVO;
import com.sinotn.id.SeqUUIDGenerator;
import com.sinotn.spring.jdbc.ParameterSetter;

public class DefaultExamServerDao extends JdbcDaoSupport implements ExamServerDao {
	private String schema;
	private String tableName="km_exam_server";

	public void setSchema(String schema) {
		this.schema = schema;
	}
	
	@Override
	public int insert(ExamServerVO examServerVO) {
		StringBuilder sb=new StringBuilder();
		sb.append("INSERT INTO ").append(this.schema).append(".").append(this.tableName);
		sb.append(" (").append(ExamServerExtractor.getColumns()).append(") VALUES (?,?,?,?,?,?,?,?,?,?)");
		if(examServerVO.getId()==null){
			examServerVO.setId(SeqUUIDGenerator.genSequenceUUID());
		}
		if(examServerVO.getRegTime()==null){
			examServerVO.setRegTime(new Date());
		}
		ParameterSetter ps=new ParameterSetter(10);
		//id,backup_time,exam_server_name,ip_addr,server_port,
		ps.set(examServerVO.getId());
		ps.set(examServerVO.getBackupTime()==null?0L:examServerVO.getBackupTime().getTime());
		ps.set(examServerVO.getExamServerName());
		ps.set(examServerVO.getIpAddr());
		ps.set(examServerVO.getServerPort());
		
		//room_count,reg_time,last_conn_time,server_status,client_open_count
		ps.set(examServerVO.getRoomCount());
		ps.set(examServerVO.getRegTime().getTime());
		ps.set(examServerVO.getLastConnTime()==null?0L:examServerVO.getLastConnTime().getTime());
		ps.set(examServerVO.getServerStatus());
		ps.set(examServerVO.getClientOpenCount());
		return this.getJdbcTemplate().update(sb.toString(), ps);
	}

	@Override
	public int update(ExamServerVO examServerVO) {
		ParameterSetter ps=new ParameterSetter(9);
		StringBuilder sb=new StringBuilder();
		sb.append("UPDATE ").append(this.schema).append(".").append(this.tableName);
		sb.append(" SET backup_time=?,exam_server_name=?,ip_addr=?,server_port=?,room_count=?,last_conn_time=?,server_status=?,client_open_count=?");
		ps.set(examServerVO.getBackupTime()==null?0L:examServerVO.getBackupTime().getTime());
		ps.set(examServerVO.getExamServerName());
		ps.set(examServerVO.getIpAddr());
		ps.set(examServerVO.getServerPort());
		ps.set(examServerVO.getRoomCount());
		ps.set(examServerVO.getLastConnTime()==null?0L:examServerVO.getLastConnTime().getTime());
		ps.set(examServerVO.getServerStatus());
		ps.set(examServerVO.getClientOpenCount());
		sb.append(" WHERE id=?");
		ps.set(examServerVO.getId());
		
		return this.getJdbcTemplate().update(sb.toString(), ps);
	}

	@Override
	public List<ExamServerVO> selects() {
		StringBuilder sb=new StringBuilder();
		sb.append("SELECT ").append(ExamServerExtractor.getColumns());
		sb.append(" FROM ").append(this.schema).append(".").append(this.tableName);
		
		return this.getJdbcTemplate().query(sb.toString(), new ExamServerExtractor());
	}

	@Override
	public int delete(String serverId) {
		StringBuilder sb=new StringBuilder();
		sb.append("DELETE FROM ").append(this.schema).append(".").append(this.tableName);
		sb.append(" WHERE id=?");
		ParameterSetter ps=new ParameterSetter(1);
		ps.set(serverId);
		return this.getJdbcTemplate().update(sb.toString(), ps);
	}
}
